/**
 * 
 */
package com.nh.orderAction;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang.StringUtils;
import org.hibernate.Criteria;
import org.hibernate.HibernateException;
import org.hibernate.criterion.Projections;

import com.nh.dao.BaseDao;
import com.nh.orderEntity.TableLodgeEntity;
import com.nh.orderEntity.VegetablesEntity;
import com.nh.orderEntity.XFBomEntity;
import com.nh.orderEntity.XfEntity;

/**
 * 项目名称：OrderingMiddleware
 * 
 * 类名称：OrderingAction
 * 
 * 类描述：
 * 
 * 创建人：李兴建
 * 
 * E-mail:lxj08193@163.com
 * 
 * 创建时间：2012-12-19 下午11:38:46
 * 
 * 修改人：李兴建
 * 
 * 修改时间：2012-12-19 下午11:38:46
 * 
 * 修改备注：
 * 
 * @version
 */
public class OrderingAction extends OrderBaseAction {
	// 使用不编码字符 ︷︸︵´︶

	// 格式：0001︶主食类︶00017︶菜名称17︶1︶107︵

	// 一条菜单格式

	// ----------------基础信息--------------------
	// private String floorCode;// 楼层code
	private String floorTitle;// 楼层名称
	private String tabelId; // 桌子id
	private String tabelCode; // 桌子code
	private String tabelTitle;// 桌子名称
	private String tabelPeople;// 桌子客容量
	private String serverLoginName;// 服务员登录名
	private String remake;// 备注
	private String increasedDishXfid;// 不为空为加菜

	private String price; // 提交价格
	private String peopleCount; // 客人数量

	// ----------------菜单信息--------------------
	private String munString;
	Map<String, List<TempVo>> map = new HashMap<String, List<TempVo>>();

	/**
	 * 提交订单
	 */
	public void submitOrderInfo() {
		String tag = "failure";// success
		String info = "";

		BaseDao baseDao = this.getBaseService().getBaseDao();
		baseDao.getSession().beginTransaction();
		try {
			XfEntity entity = null;

			if (StringUtils.isNotEmpty(increasedDishXfid)) {// 加菜
				List<XfEntity> lsitTemp = this.getBaseService().findByProperty("xfid", Integer.parseInt(increasedDishXfid), XfEntity.class);
				if (lsitTemp.size() > 0) {
					entity = lsitTemp.get(0);
				}
			} else {// 新添加菜
				entity = new XfEntity();
			}
			if (null == entity) {
				tag = "failure";
				info = "加菜失败，未找到已提交菜单， 请联系管理员";
				String json = "[{\"info\":\"" + tag + "\",\"msg\":\"" + info + "\"}]";
				writResponse(json);
				return;
			}

			entity.setCode(getMaxCode());
			entity.setPeopleCount(Integer.parseInt(peopleCount));
			entity.setTableId(Integer.parseInt(tabelId));
			entity.setRemake(remake);
			entity.setAccountState(false);
			baseDao.getSession().save(entity);

			List<TableLodgeEntity> tabels = this.getBaseService().findByProperty("tid", entity.getTableId(), TableLodgeEntity.class);
			if (tabels.size() > 0) {
				TableLodgeEntity tableLodgeEntity = tabels.get(0);
				// 检查桌子是否在订 否则提示用户重新选择
				/*
				 * if (tableLodgeEntity.getState() == 1) { tag = "failure"; info
				 * = "你选择的桌位号未结账，请重新选择"; String json = "[{\"info\":\"" + tag +
				 * "\",\"msg\":\"" + info + "\"}]";
				 * this.getBaseService().delete(entity); writResponse(json);
				 * return; }
				 */

				tableLodgeEntity.setState(1);
				tableLodgeEntity.setPeopleCount(entity.getPeopleCount());
				this.getBaseService().saveOrUpdate(tableLodgeEntity, TableLodgeEntity.class);
			}

			List<XFBomEntity> list = parseXF(munString, entity.getXfid());

			if (list.size() < 0) {
				tag = "failure";// success
				info = "订单提交失败";
				baseDao.getSession().beginTransaction().rollback();
			} else {
				for (XFBomEntity bomEntity : list) {
					baseDao.getSession().save(bomEntity);
				}
				baseDao.getSession().beginTransaction().commit();
			}

			System.out.println(munString);
			tag = "success";
			info = "订单提交成功 ";

			// 打印
			// String title = floorTitle + "/" + tabelTitle + " 人数:" +
			// peopleCount + " 编号:" + entity.getCode();
			// 编号改成时间
			SimpleDateFormat dateFm = new SimpleDateFormat("MM-dd HH:mm"); // 格式化当前系统日期
			String dateTime = dateFm.format(new java.util.Date());
			String title = floorTitle + "/" + tabelTitle + " 人数:" + peopleCount + " 时间:" + dateTime;
			TestPrint testPrint = new TestPrint();
			testPrint.test(map, title, remake);
			testPrint = new TestPrint();
			testPrint.test(map, title, remake);
			//Iterator it = map.keySet().iterator();

			/*类别不要了
			while (it.hasNext()) {
				String key = (String) it.next();
				Map<String, List<TempVo>> tempMap = new HashMap<String, List<TempVo>>();
				tempMap.put(key, map.get(key));
				title = floorTitle + "/" + tabelTitle + " 人数:" + peopleCount + " 时间:" + dateTime;
				testPrint = new TestPrint();
				testPrint.test(tempMap, title, remake);
			}
			*/
			//添加每个菜单独打一条记录-20130528
			Iterator it = map.keySet().iterator();
			while (it.hasNext()) {
				String key = (String) it.next();				
				title = floorTitle + "/" + tabelTitle + " 人数:" + peopleCount + " 时间:" + dateTime;
				for(TempVo vo:map.get(key)){
					Map<String, List<TempVo>> tempMap = new HashMap<String, List<TempVo>>();
					List<TempVo> tempList= new ArrayList<TempVo>();
					tempList.add(vo);
					tempMap.put(key, tempList); 
				    testPrint = new TestPrint();
				    testPrint.test(tempMap, title, remake);
				}
			}

		} catch (Exception e) {
			baseDao.getSession().beginTransaction().rollback();
			info = "订单提交失败";
			e.printStackTrace();
		}

		String json = "[{\"info\":\"" + tag + "\",\"msg\":\"" + info + "\"}]";
		writResponse(json);

	}

	// [str appendString:[mun objectForKey:ORDER_MENU_TYPE_CODE]];
	// [str appendString:@"︶"];
	// [str appendString:[mun objectForKey:ORDER_MENU_TYPE_TITLE]];
	// [str appendString:@"︶"];
	// [str appendString:[mun objectForKey:ORDER_MENU_CODE]];
	// [str appendString:@"︶"];
	// [str appendString:[mun objectForKey:ORDER_MENU_TITLE]];
	// [str appendString:@"︶"];
	// [str appendString:[mun objectForKey:ORDER_MENU_COUNT]];
	// [str appendString:@"︶"];
	// [str appendString:[mun objectForKey:ORDER_MENU_PRICE]];
	// [str appendString:@"︶"];
	// [str appendString:[mun objectForKey:ORDER_MENU_PRICE]];
	// [str appendString:@"︶"];
	// [str appendString:[mun objectForKey:ORDER_MENU_REMAKE]];
	// [str appendString:@"︶"];
	// [str appendString:[mun objectForKey:ORDER_MENU_STATE]];

	public List<XFBomEntity> parseXF(String str, int xfdid) {
		List<XFBomEntity> list = new ArrayList<XFBomEntity>();
		map = new HashMap<String, List<TempVo>>();
		List<TempVo> list2;

		if (!str.contains("︵")) {
			return list;
		}
		String[] ss = str.split("︵");
		for (String xfstr : ss) {
			if (org.apache.commons.lang.StringUtils.isEmpty(xfstr)) {
				continue;
			}
			String[] xf = xfstr.split("︶");
			String ORDER_MENU_TYPE_CODE = xf[0];
			String ORDER_MENU_TYPE_TITLE = xf[1];
			String ORDER_MENU_CODE = xf[2];
			String ORDER_MENU_TITLE = xf[3];
			String ORDER_MENU_COUNT = xf[4];
			String ORDER_MENU_PRICE = xf[5];
			String ORDER_MENU_REMAKE = xf[6];
			String ORDER_MENU_STATE = xf[7];
			String ORDER_MENU_ID = xf[8];
			XFBomEntity entity = new XFBomEntity();
			entity.setXfid(xfdid);
			entity.setMeunId(Integer.parseInt(ORDER_MENU_ID));
			entity.setCount(Integer.parseInt(ORDER_MENU_COUNT));
			if ("1".equals(ORDER_MENU_STATE)) {
				entity.setRemake("赠品");
				// double d = (Double.parseDouble(ORDER_MENU_PRICE));
				entity.setPrice(0);
			} else {
				entity.setRemake("");
				double d = (Double.parseDouble(ORDER_MENU_PRICE));
				entity.setPrice((int) d);
			}

			entity.setZpRemake(ORDER_MENU_STATE);
			entity.setXfRemake(ORDER_MENU_REMAKE);
			list.add(entity);

			// 用于打印
			if (map.containsKey(ORDER_MENU_TYPE_TITLE)) {
				list2 = map.get(ORDER_MENU_TYPE_TITLE);
			} else {
				list2 = new ArrayList<TempVo>();
			}
			// 单位
			List<VegetablesEntity> vs = this.getBaseService().findByProperty("cid", entity.getMeunId(), VegetablesEntity.class);
			TempVo tempVo = new TempVo();
			tempVo.setTitle(ORDER_MENU_TITLE);
			if (vs.size() > 0) {
				VegetablesEntity vegetablesEntity = vs.get(0);
				tempVo.setCout(ORDER_MENU_COUNT + vegetablesEntity.getUnit());
			} else {
				tempVo.setCout(ORDER_MENU_COUNT);
			}
			tempVo.setRemake(ORDER_MENU_REMAKE);
			list2.add(tempVo);
			map.put(ORDER_MENU_TYPE_TITLE, list2);
			// 用于打印

		}
		return list;
	}

	public String getMaxCode() {
		Criteria criteria = this.createCriteria(XfEntity.class);
		String str = null;
		try {
			str = (String) criteria.setProjection(Projections.projectionList().add(Projections.max("code"))).uniqueResult();
		} catch (HibernateException e) {
			e.printStackTrace();
		}
		if (org.apache.commons.lang.StringUtils.isEmpty(str)) {
			str = "00000000000";
		}
		str = str.substring(8, 11);
		SimpleDateFormat time = new SimpleDateFormat("yyyyMMdd");
		String dateTime = time.format(new Date());
		dateTime = dateTime + str;
		dateTime = (Long.parseLong(dateTime) + 1) + "";
		return dateTime;
	}

	public static void main(String[] args) {
		String str = "20121227002";
		str = str.substring(8, 11);

		SimpleDateFormat time = new SimpleDateFormat("yyyyMMdd");
		String dateTime = time.format(new Date());
		System.out.println(dateTime + str);
	}

	/**
	 * @return the floorTitle
	 */
	public String getFloorTitle() {
		return floorTitle;
	}

	/**
	 * @param floorTitle
	 *            the floorTitle to set
	 */
	public void setFloorTitle(String floorTitle) {
		this.floorTitle = floorTitle;
	}

	/**
	 * @return the tabelCode
	 */
	public String getTabelCode() {
		return tabelCode;
	}

	/**
	 * @param tabelCode
	 *            the tabelCode to set
	 */
	public void setTabelCode(String tabelCode) {
		this.tabelCode = tabelCode;
	}

	/**
	 * @return the tabelTitle
	 */
	public String getTabelTitle() {
		return tabelTitle;
	}

	/**
	 * @param tabelTitle
	 *            the tabelTitle to set
	 */
	public void setTabelTitle(String tabelTitle) {
		this.tabelTitle = tabelTitle;
	}

	/**
	 * @return the tabelPeople
	 */
	public String getTabelPeople() {
		return tabelPeople;
	}

	/**
	 * @param tabelPeople
	 *            the tabelPeople to set
	 */
	public void setTabelPeople(String tabelPeople) {
		this.tabelPeople = tabelPeople;
	}

	/**
	 * @return the serverLoginName
	 */
	public String getServerLoginName() {
		return serverLoginName;
	}

	/**
	 * @param serverLoginName
	 *            the serverLoginName to set
	 */
	public void setServerLoginName(String serverLoginName) {
		this.serverLoginName = serverLoginName;
	}

	/**
	 * @return the price
	 */
	public String getPrice() {
		return price;
	}

	/**
	 * @param price
	 *            the price to set
	 */
	public void setPrice(String price) {
		this.price = price;
	}

	/**
	 * @return the peopleCount
	 */
	public String getPeopleCount() {
		return peopleCount;
	}

	/**
	 * @param peopleCount
	 *            the peopleCount to set
	 */
	public void setPeopleCount(String peopleCount) {
		this.peopleCount = peopleCount;
	}

	/**
	 * @return the munString
	 */
	public String getMunString() {
		return munString;
	}

	/**
	 * @param munString
	 *            the munString to set
	 */
	public void setMunString(String munString) {
		this.munString = munString;
	}

	/**
	 * @return the remake
	 */
	public String getRemake() {
		return remake;
	}

	/**
	 * @param remake
	 *            the remake to set
	 */
	public void setRemake(String remake) {
		this.remake = remake;
	}

	/**
	 * @return the tabelId
	 */
	public String getTabelId() {
		return tabelId;
	}

	/**
	 * @param tabelId
	 *            the tabelId to set
	 */
	public void setTabelId(String tabelId) {
		this.tabelId = tabelId;
	}

	public String getIncreasedDishXfid() {
		return increasedDishXfid;
	}

	public void setIncreasedDishXfid(String increasedDishXfid) {
		this.increasedDishXfid = increasedDishXfid;
	}

	public Map<String, List<TempVo>> getMap() {
		return map;
	}

	public void setMap(Map<String, List<TempVo>> map) {
		this.map = map;
	}

}
